/*
============================================================================
 Name        : dd_vector.h
 Author      : Jian (raphyer@hotmail.com)
 Version     :
 Copyright   : GPL
 Description : dd_vector.h - header file
============================================================================
*/

#ifndef DD_VECTOR_H
#define DD_VECTOR_H

#include "dd_define.h"

#define __DD_BEGIN_DECLS

typedef struct __dd_vector dd_vector;
typedef int dd_vector_data_cmp(const void* data1, const void* data2) ;
typedef int dd_vector_key_cmp(const void* key, const void* data) ;


struct __dd_vector {
	void**	ptr ;
	int	count ;
	int	allocSize ;
	int	granularity ;
} ;

dd_vector* dd_vector_create(int granularity) ;

void dd_vector_destroy(dd_vector* vec, void (*free_fun)(void*)) ;

int dd_vector_count(const dd_vector* vec) ;

void* dd_vector_get(const dd_vector* vec, DD_UINT index) ;

void dd_vector_set(dd_vector* vec, DD_UINT index, void* data);

void dd_vector_append(dd_vector* vec, void* data) ;

void* dd_vector_delete_by_index(dd_vector* vec, DD_UINT index) ;

void* dd_vector_delete(dd_vector* vec, void* data);

void dd_vector_insert(dd_vector* vec, DD_UINT insertIndex, void* data) ;

void dd_vector_expand(dd_vector* vec, int expandSize) ;

void dd_vector_reset(dd_vector* vec, void (*free_fun)(void*)) ;

int dd_vector_find(dd_vector* vec, void* data) ;

void dd_vector_swap(dd_vector* vec, DD_UINT index1, DD_UINT index2) ;


/* Below macros is for the fast operation without any check. */

#define dd_vector_getfast(vec, index) (*((vec)->ptr + (index)))

#define dd_vector_setfast(vec, index, data) (*((vec)->ptr + (index)) = (data))

/* the flowing 2 function will be faster if the big vec is sorted */
int dd_vector_find_by_key(const dd_vector* vec, const void* key, dd_vector_key_cmp* compare, int isSort) ;

void* dd_vector_delete_by_key(dd_vector* vec, const void* key, dd_vector_key_cmp* compare, int isSort) ;

/* From a un-sorted vector to a sorted vector */
void dd_vector_sort(dd_vector* vec, dd_vector_data_cmp* compare) ;

/* insert data in a sort vector*/
void dd_vector_insert_by(dd_vector* vec, void* data, dd_vector_data_cmp* compare) ;


#ifdef __DD_TEST

DD_BOOL dd_vector_test() ;

void dd_vector_dump_file(const dd_vector* vec, const char* file_name) ;

dd_vector* dd_vector_read_file(const char* file_name) ;

#endif


#define __DD_END_DECLS

#endif	/* DD_VECTOR_H */

/* End of File */
